package com.app.service.Impl;

import cn.hutool.core.lang.Console;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.app.common.RandomNumUtils;
import com.app.common.Result;
import com.app.event.ComplaintEvent;
import com.app.mapper.*;
import com.app.model.*;
import com.app.service.ComplaintService;
import com.app.service.RoleService;
import com.app.util.CommonHelp;
import com.app.vo.ComplainVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Random;
@Slf4j
@Service
public class ComplaintServiceImpl implements ComplaintService {
    @Autowired
    private ComplaintMapper complaintMapper;
    @Autowired
    private ComplaintUrlMapper complaintUrlMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private AdminUserMapper adminUserMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleService roleService;
    @Autowired
    private ApplicationContext applicationContext;
    @Override
    public String addComplaint(ComplaintModel model) {
        String code = RandomNumUtils.getCode();
        while (selectSchedule(code) != null) {
            code = RandomNumUtils.getCode();
        }
        model.setCode(code);
        model.setCreateTime(CommonHelp.getNowTimestamp());
        if (model.getSituation() != null) {
            String[] situation = model.getSituation();
            StringBuilder s = new StringBuilder();
            for (int i = 0; i < situation.length; i++) {
                s.append(situation[i]).append(",");
            }
            model.setSituate(s.toString());
        }
        if (model.getInformation().contains("材料") || model.getInformation().contains("租赁")) {
            model.setStatus(2);
            model.setRemark("您的投诉不属于劳动监察大队业务受理范围，请拨打12348反映您的情况。");
            model.setExamine(1);
            model.setProcessingTime(CommonHelp.getNowTimestamp());
        }
        model.setWarningStatus(1);
        complaintMapper.insert(model);
        if (model.getPic() != null) {
            String[] pic = model.getPic();
            StringBuilder s1 = new StringBuilder();
            for (int i = 0; i < pic.length; i++) {
                if (i == pic.length - 1) {
                    s1.append(pic[i]);
                } else {
                    s1.append(pic[i]).append(",");
                }
            }
            ComplainUrlModel complainUrlModel = new ComplainUrlModel();
            complainUrlModel.setUrl(s1.toString());
            complainUrlModel.setType(1);
            complainUrlModel.setComplainId(model.getId());
            complainUrlModel.setCreateTime(CommonHelp.getNowTimestamp());
            complaintUrlMapper.insert(complainUrlModel);
        }
        if (model.getVideo() != null) {
            String[] pic = model.getVideo();
            StringBuilder s1 = new StringBuilder();
            for (int i = 0; i < pic.length; i++) {
                if (i == pic.length - 1) {
                    s1.append(pic[i]);
                } else {
                    s1.append(pic[i]).append(",");
                }
            }
            ComplainUrlModel complainUrlModel = new ComplainUrlModel();
            complainUrlModel.setUrl(s1.toString());
            complainUrlModel.setType(2);
            complainUrlModel.setComplainId(model.getId());
            complainUrlModel.setCreateTime(CommonHelp.getNowTimestamp());
            complaintUrlMapper.insert(complainUrlModel);
        }
        if (model.getAudio() != null) {
            String[] pic = model.getAudio();
            StringBuilder s1 = new StringBuilder();
            for (int i = 0; i < pic.length; i++) {
                if (i == pic.length - 1) {
                    s1.append(pic[i]);
                } else {
                    s1.append(pic[i]).append(",");
                }
            }
            ComplainUrlModel complainUrlModel = new ComplainUrlModel();
            complainUrlModel.setUrl(s1.toString());
            complainUrlModel.setType(3);
            complainUrlModel.setComplainId(model.getId());
            complainUrlModel.setCreateTime(CommonHelp.getNowTimestamp());
            complaintUrlMapper.insert(complainUrlModel);
        }
        if (model.getPDF() != null) {
            String[] pdf = model.getPDF();
            StringBuilder s1 = new StringBuilder();
            for (int i = 0; i < pdf.length; i++) {
                if (i == pdf.length - 1) {
                    s1.append(pdf[i]);
                } else {
                    s1.append(pdf[i]).append(",");
                }
            }
            ComplainUrlModel complainUrlModel = new ComplainUrlModel();
            complainUrlModel.setUrl(s1.toString());
            complainUrlModel.setType(4);
            complainUrlModel.setComplainId(model.getId());
            complainUrlModel.setCreateTime(CommonHelp.getNowTimestamp());
            complaintUrlMapper.insert(complainUrlModel);
        }
        return code;
    }

    @Override
    public List<ComplaintModel> complaintList(Integer userId) {
        QueryWrapper<ComplaintModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).orderByDesc("id");
        List<ComplaintModel> complaintModels = complaintMapper.selectList(queryWrapper);
        for (int i = 0; i < complaintModels.size(); i++) {
            if (complaintModels.get(i).getExamine() != null && complaintModels.get(i).getExamine() != 1 && complaintModels.get(i).getStatus() != 1) {
                complaintModels.get(i).setStatus(4);
            }
            if (complaintModels.get(i).getExamine() != null && complaintModels.get(i).getExamine() == 0) {
                if (complaintModels.get(i).getStatus() == 1) {
                    complaintModels.get(i).setStatusText("待受理");
                } else {
                    complaintModels.get(i).setStatusText("受理中");
                }
            } else if (complaintModels.get(i).getExamine() != null) {
                if (complaintModels.get(i).getStatus() == 1) {
                    complaintModels.get(i).setStatusText("待受理");
                }
                if (complaintModels.get(i).getStatus() == 2) {
                    complaintModels.get(i).setStatusText("已办理");
                }
                if (complaintModels.get(i).getStatus() == 4) {
                    complaintModels.get(i).setStatusText("受理中");
                }
            }
        }
        return complaintModels;
    }

    @Override
    public ComplaintModel selectSchedule(String code) {
        QueryWrapper<ComplaintModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("code", code);
        ComplaintModel model = complaintMapper.selectOne(queryWrapper);
        if (model == null) {
            return null;
        }
        model.setIdCardBack("");
        model.setIdCardFront("");
        QueryWrapper<ComplainUrlModel> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("complain_id", model.getId());
        List<ComplainUrlModel> complainUrlModels = complaintUrlMapper.selectList(queryWrapper1);
        for (int i = 0; i < complainUrlModels.size(); i++) {
            if (complainUrlModels.get(i).getType() == 1) {
                model.setPicUrl(complainUrlModels.get(i).getUrl());
            }
            if (complainUrlModels.get(i).getType() == 2) {
                model.setVideoUrl(complainUrlModels.get(i).getUrl());
            }
            if (complainUrlModels.get(i).getType() == 3) {
                model.setAudioUrl(complainUrlModels.get(i).getUrl());
            }
            if (complainUrlModels.get(i).getType() == 4) {
                model.setPdfUrl(complainUrlModels.get(i).getUrl());
            }
        }
        if (model.getSituate() != null && !model.getSituate().equals("")) {
            String situation = model.getSituate();
            String[] split = situation.split(",");
            if (split.length == 0) {
                return null;
            }
            String[] situationText = new String[split.length];
            List<CategoryModel> categoryModels = categoryMapper.selectList(null);
            for (int i = 0; i < split.length; i++) {
                for (int i1 = 0; i1 < categoryModels.size(); i1++) {
                    if (Integer.valueOf(split[i]).equals(categoryModels.get(i1).getId())) {
                        situationText[i] = categoryModels.get(i1).getName();
                    }
                }
            }
            model.setSituationText(situationText);
        }
        if (model.getExamine() != 1 && model.getStatus() != 1) {
            model.setStatus(4);
            return model;
        }
        return model;
    }

    @Override
    public ComplaintModel selectSchedule(String code, Integer adminId) {
        QueryWrapper<ComplaintModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("code", code);
        ComplaintModel model = complaintMapper.selectOne(queryWrapper);
        if (model == null) {
            return null;
        }
        model.setIdCardBack("");
        model.setIdCardFront("");
        QueryWrapper<ComplainUrlModel> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("complain_id", model.getId());
        List<ComplainUrlModel> complainUrlModels = complaintUrlMapper.selectList(queryWrapper1);
        for (int i = 0; i < complainUrlModels.size(); i++) {
            if (complainUrlModels.get(i).getType() == 1) {
                model.setPicUrl(complainUrlModels.get(i).getUrl());
            }
            if (complainUrlModels.get(i).getType() == 2) {
                model.setVideoUrl(complainUrlModels.get(i).getUrl());
            }
            if (complainUrlModels.get(i).getType() == 3) {
                model.setAudioUrl(complainUrlModels.get(i).getUrl());
            }
            if (complainUrlModels.get(i).getType() == 4) {
                model.setPdfUrl(complainUrlModels.get(i).getUrl());
            }
        }
        if (model.getSituate() != null && !model.getSituate().equals("")) {
            String situation = model.getSituate();
            String[] split = situation.split(",");
            if (split.length == 0) {
                return null;
            }
            String[] situationText = new String[split.length];
            List<CategoryModel> categoryModels = categoryMapper.selectList(null);
            for (int i = 0; i < split.length; i++) {
                for (int i1 = 0; i1 < categoryModels.size(); i1++) {
                    if (Integer.valueOf(split[i]).equals(categoryModels.get(i1).getId())) {
                        situationText[i] = categoryModels.get(i1).getName();
                    }
                }
            }
            model.setSituationText(situationText);
        }
        AdminUserModel adminUserModel = adminUserMapper.selectById(adminId);
        UserModel userModel = userMapper.selectById(adminUserModel.getUserId());
        if (userModel != null) {
            model.setProcessedName(userModel.getUserName());
            model.setProcessedMobile(userModel.getMobile());
        }
        if (model.getExamine() != 1 && model.getStatus() != 1) {
            model.setStatus(4);
            return model;
        }
        return model;
    }

    @Override
    public ComplaintModel selectDetail(Integer id) {
        ComplaintModel model = complaintMapper.selectById(id);
        if (model == null) {
            return null;
        }
        model.setIdCardBack("");
        model.setIdCardFront("");
        if (model.getExamine() != null && model.getExamine() != 1 && model.getStatus() != 1) {
            model.setStatus(4);
        }
        QueryWrapper<ComplainUrlModel> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("complain_id", model.getId()).orderByDesc("id");
        List<ComplainUrlModel> complainUrlModels = complaintUrlMapper.selectList(queryWrapper1);
        for (int i = 0; i < complainUrlModels.size(); i++) {
            if (complainUrlModels.get(i).getType() == 1) {
                model.setPicUrl(complainUrlModels.get(i).getUrl());
            }
            if (complainUrlModels.get(i).getType() == 2) {
                model.setVideoUrl(complainUrlModels.get(i).getUrl());
            }
            if (complainUrlModels.get(i).getType() == 3) {
                model.setAudioUrl(complainUrlModels.get(i).getUrl());
            }
            if (complainUrlModels.get(i).getType() == 4) {
                model.setPdfUrl(complainUrlModels.get(i).getUrl());
            }
        }
        if (model.getSituate() != null && !model.getSituate().equals("")) {
            String situation = model.getSituate();
            String[] split = situation.split(",");
            String[] situationText = new String[split.length];
            List<CategoryModel> categoryModels = categoryMapper.selectList(null);
            for (int i = 0; i < split.length; i++) {
                for (int i1 = 0; i1 < categoryModels.size(); i1++) {
                    if (Integer.valueOf(split[i]).equals(categoryModels.get(i1).getId())) {
                        situationText[i] = categoryModels.get(i1).getName();
                    }
                }
            }
            model.setSituationText(situationText);
        }
        return model;
    }

    @Override
    public ComplaintModel selectDetail(Integer id, Integer adminId) {
        ComplaintModel model = complaintMapper.selectById(id);
        if (model == null) {
            return null;
        }
        model.setIdCardBack("");
        model.setIdCardFront("");
        if (model.getExamine() != null && model.getExamine() != 1 && model.getStatus() != 1) {
            model.setStatus(4);
        }
        QueryWrapper<ComplainUrlModel> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("complain_id", model.getId()).orderByDesc("id");
        List<ComplainUrlModel> complainUrlModels = complaintUrlMapper.selectList(queryWrapper1);
        for (int i = 0; i < complainUrlModels.size(); i++) {
            if (complainUrlModels.get(i).getType() == 1) {
                model.setPicUrl(complainUrlModels.get(i).getUrl());
            }
            if (complainUrlModels.get(i).getType() == 2) {
                model.setVideoUrl(complainUrlModels.get(i).getUrl());
            }
            if (complainUrlModels.get(i).getType() == 3) {
                model.setAudioUrl(complainUrlModels.get(i).getUrl());
            }
            if (complainUrlModels.get(i).getType() == 4) {
                model.setPdfUrl(complainUrlModels.get(i).getUrl());
            }
        }
        if (model.getSituate() != null && !model.getSituate().equals("")) {
            String situation = model.getSituate();
            String[] split = situation.split(",");
            String[] situationText = new String[split.length];
            List<CategoryModel> categoryModels = categoryMapper.selectList(null);
            for (int i = 0; i < split.length; i++) {
                for (int i1 = 0; i1 < categoryModels.size(); i1++) {
                    if (Integer.valueOf(split[i]).equals(categoryModels.get(i1).getId())) {
                        situationText[i] = categoryModels.get(i1).getName();
                    }
                }
            }
            model.setSituationText(situationText);
        }
        model.setAdminId(adminId);
        return model;
    }

    @Override
    public void deleteComplaintList(Integer id) {
        complaintMapper.deleteById(id);
    }

    @Override
    public String updateComplaintCircleStatus(Integer id, Integer circleStatus, String url) {
        ComplaintModel complaintModel = complaintMapper.selectById(id);
        if (circleStatus != null) {
            complaintModel.setCircleStatus(circleStatus);
        }
        if (url != null) {
            complaintModel.setCircleFile(url);
        }
        complaintModel.setUpdateTime(CommonHelp.getNowTimestamp());
        int flag = complaintMapper.updateById(complaintModel);
        applicationContext.publishEvent(new ComplaintEvent(complaintModel));
        if (flag == 0) {
            return "操作失败";
        }
        return "操作成功";

    }

    @Override
    public String updateReclassifyIndustry(Integer id, Integer industryId) {
        ComplaintModel complaintModel = complaintMapper.selectById(id);
        complaintModel.setReclassifyIndustry(industryId);
        complaintModel.setUpdateTime(CommonHelp.getNowTimestamp());
        int flag = complaintMapper.updateById(complaintModel);
        applicationContext.publishEvent(new ComplaintEvent(complaintModel));
        if (flag == 0) {
            return "操作失败";
        }
        return "操作成功";
    }

    @Override
    public void examineComplaint(Integer id, Integer status, String remark, String handleRemark, String rejectRemark, AdminUserModel user) {
        ComplaintModel model = complaintMapper.selectById(id);
        if (model.getExamine() == 2) {
            model.setExamine(0);
        }
        if (user != null && user.getId() != null) {
            model.setProcessedId(user.getId());
        }
        model.setStatus(status);
        if (handleRemark != null && status == 4) {//受理中
            assert user != null;
            model.setProcessedName(user.getRealName());
            model.setHandleRemark(handleRemark);
        }
        if (remark != null && status == 2) {//办结
            assert user != null;
            model.setRejectRemark(user.getRealName());
            model.setRemark(remark);
        }
        model.setProcessingTime(CommonHelp.getNowTimestamp());
        complaintMapper.updateById(model);
        if (model.getIsJiuquan() != null) {
            try {
                applicationContext.publishEvent(new ComplaintEvent(model));
            }catch (Exception e){
                log.info(e.toString());
            }
        }
    }

    @Override
    public ComplainVo selectComplaintList(Integer page, Integer limit, String keyWord, String selectCrtTime, AdminUserModel user, Integer clue, Integer status, Integer examine, Integer warningStatus) {
        page = (page - 1) * limit;
        String startTime = null;
        String endTime = null;
        if (selectCrtTime != null && !selectCrtTime.equals("")) {
            String[] split = selectCrtTime.split(",");
            startTime = split[0] + " 00:00:00";
            endTime = split[1] + " 23:59:59";
        }
        QueryWrapper<CategoryModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", 10);
        List<CategoryModel> categoryModelList = categoryMapper.selectList(queryWrapper);
        List<ComplaintModel> complaintModels = complaintMapper.selectComplaintList(page, limit, keyWord, startTime, endTime, user.getId(), user.getRoles(), clue, status, examine, warningStatus);
        for (int i = 0; i < complaintModels.size(); i++) {
            if (complaintModels.get(i).getCircleStatus() != null && complaintModels.get(i).getCircleStatus() == 1) {
                complaintModels.get(i).setCircleStatusText("当场支付");
            } else if (complaintModels.get(i).getCircleStatus() != null && complaintModels.get(i).getCircleStatus() == 2) {
                complaintModels.get(i).setCircleStatusText("达成支付协议");
            } else if (complaintModels.get(i).getCircleStatus() != null && complaintModels.get(i).getCircleStatus() == 3) {
                complaintModels.get(i).setCircleStatusText("书面投诉立案");
            } else if (complaintModels.get(i).getCircleStatus() != null && complaintModels.get(i).getCircleStatus() == 4) {
                complaintModels.get(i).setCircleStatusText("告知其他途径");
            }
            for (int i1 = 0; i1 < categoryModelList.size(); i1++) {
                if (complaintModels.get(i).getIndustryIndex() != null && complaintModels.get(i).getIndustryIndex().equals(categoryModelList.get(i1).getId())) {
                    complaintModels.get(i).setIndustryText(categoryModelList.get(i1).getName());
                }
                if (complaintModels.get(i).getReclassifyIndustry() != null && complaintModels.get(i).getReclassifyIndustry().equals(categoryModelList.get(i1).getId())) {
                    complaintModels.get(i).setReclassifyIndustryText(categoryModelList.get(i1).getName());
                }
            }
            if (complaintModels.get(i).getAdminId() == null) {
                complaintModels.get(i).setAdminId(user.getId());
            }
            if (complaintModels.get(i).getProcessedId() != null && complaintModels.get(i).getProcessedId() != 1) {
                Integer adminId = complaintModels.get(i).getProcessedId();
                AdminUserModel adminUserModel = adminUserMapper.selectById(adminId);
                if (adminUserModel != null) {
                    UserModel userModel = userMapper.selectById(adminUserModel.getUserId());
                    if (userModel != null) {
                        complaintModels.get(i).setProcessedName(userModel.getUserName());
                        complaintModels.get(i).setProcessedMobile(userModel.getMobile());
                    }
                }
            } else if (complaintModels.get(i).getProcessedId() != null && complaintModels.get(i).getProcessedId() == 1) {
                complaintModels.get(i).setProcessedName("超级管理员");
                complaintModels.get(i).setProcessedMobile("");
            }
            if (complaintModels.get(i).getStatus() == 1) {
                complaintModels.get(i).setStatusText("待受理");
            }
            if (complaintModels.get(i).getStatus() == 2) {
                complaintModels.get(i).setStatusText("已办理");
            }
            if (complaintModels.get(i).getStatus() == 4) {
                complaintModels.get(i).setStatusText("受理中");
            }
            if (complaintModels.get(i).getType() != null) {
                if (complaintModels.get(i).getType() == 2) {
                    complaintModels.get(i).setTypeText("⾮⼯程建设领域");
                }
                if (complaintModels.get(i).getType() == 1) {
                    complaintModels.get(i).setTypeText("⼯程建设领域");
                }
            }
            if (complaintModels.get(i).getIsContract() != null) {
                if (complaintModels.get(i).getIsContract() == 1) {
                    complaintModels.get(i).setIsContractText("是");
                }
                if (complaintModels.get(i).getIsContract() == 2) {
                    complaintModels.get(i).setIsContractText("否");
                }
            }
            for (int j = 0; j < categoryModelList.size(); j++) {
                if (complaintModels.get(i).getIndustryIndex() != null && complaintModels.get(i).getIndustryIndex().equals(categoryModelList.get(j).getId())) {
                    complaintModels.get(i).setIndustryText(categoryModelList.get(j).getName());
                }
                if (complaintModels.get(i).getReclassifyIndustry() != null && complaintModels.get(i).getReclassifyIndustry().equals(categoryModelList.get(j).getId())) {
                    complaintModels.get(i).setReclassifyIndustryText(categoryModelList.get(j).getName());
                }
            }
        }
        int count = complaintMapper.selectComplaintCount(keyWord, startTime, endTime, user.getId(), user.getRoles(), clue, status, examine, warningStatus);
        ComplainVo vo = new ComplainVo();
        vo.setList(complaintModels);
        vo.setCount(count);
        return vo;
    }

    @Override
    public ComplainVo getComplaintList(Integer page, Integer limit, String keyword, String selectCrtTime, String isJiuquan, Integer clue, Integer status, Integer examine, Integer warningStatus) {
        page = (page - 1) * limit;
        String startTime = null;
        String endTime = null;
        if (selectCrtTime != null && !selectCrtTime.equals("")) {
            String[] split = selectCrtTime.split(",");
            startTime = split[0] + " 00:00:00";
            endTime = split[1] + " 23:59:59";
        }
        QueryWrapper<CategoryModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", 10);
        List<CategoryModel> categoryModelList = categoryMapper.selectList(queryWrapper);
        List<ComplaintModel> complaintModels = complaintMapper.getComplaintList(page, limit, keyword, startTime, endTime,isJiuquan, clue, status, examine, warningStatus);
        for (int i = 0; i < complaintModels.size(); i++) {
            if (complaintModels.get(i).getCircleStatus() != null && complaintModels.get(i).getCircleStatus() == 1) {
                complaintModels.get(i).setCircleStatusText("当场支付");
            } else if (complaintModels.get(i).getCircleStatus() != null && complaintModels.get(i).getCircleStatus() == 2) {
                complaintModels.get(i).setCircleStatusText("达成支付协议");
            } else if (complaintModels.get(i).getCircleStatus() != null && complaintModels.get(i).getCircleStatus() == 3) {
                complaintModels.get(i).setCircleStatusText("书面投诉立案");
            } else if (complaintModels.get(i).getCircleStatus() != null && complaintModels.get(i).getCircleStatus() == 4) {
                complaintModels.get(i).setCircleStatusText("告知其他途径");
            }
            for (int i1 = 0; i1 < categoryModelList.size(); i1++) {
                if (complaintModels.get(i).getIndustryIndex() != null && complaintModels.get(i).getIndustryIndex().equals(categoryModelList.get(i1).getId())) {
                    complaintModels.get(i).setIndustryText(categoryModelList.get(i1).getName());
                }
                if (complaintModels.get(i).getReclassifyIndustry() != null && complaintModels.get(i).getReclassifyIndustry().equals(categoryModelList.get(i1).getId())) {
                    complaintModels.get(i).setReclassifyIndustryText(categoryModelList.get(i1).getName());
                }
            }
            if (complaintModels.get(i).getProcessedId() != null && complaintModels.get(i).getProcessedId() != 1) {
                Integer adminId = complaintModels.get(i).getProcessedId();
                AdminUserModel adminUserModel = adminUserMapper.selectById(adminId);
                if (adminUserModel != null) {
                    UserModel userModel = userMapper.selectById(adminUserModel.getUserId());
                    if (userModel != null) {
                        complaintModels.get(i).setProcessedName(userModel.getUserName());
                        complaintModels.get(i).setProcessedMobile(userModel.getMobile());
                    }
                }
            } else if (complaintModels.get(i).getProcessedId() != null && complaintModels.get(i).getProcessedId() == 1) {
                complaintModels.get(i).setProcessedName("超级管理员");
                complaintModels.get(i).setProcessedMobile("");
            }
            if (complaintModels.get(i).getStatus() == 1) {
                complaintModels.get(i).setStatusText("待受理");
            }
            if (complaintModels.get(i).getStatus() == 2) {
                complaintModels.get(i).setStatusText("已办理");
            }
            if (complaintModels.get(i).getStatus() == 4) {
                complaintModels.get(i).setStatusText("受理中");
            }
            if (complaintModels.get(i).getType() != null) {
                if (complaintModels.get(i).getType() == 2) {
                    complaintModels.get(i).setTypeText("⾮⼯程建设领域");
                }
                if (complaintModels.get(i).getType() == 1) {
                    complaintModels.get(i).setTypeText("⼯程建设领域");
                }
            }
            if (complaintModels.get(i).getIsContract() != null) {
                if (complaintModels.get(i).getIsContract() == 1) {
                    complaintModels.get(i).setIsContractText("是");
                }
                if (complaintModels.get(i).getIsContract() == 2) {
                    complaintModels.get(i).setIsContractText("否");
                }
            }
            for (int j = 0; j < categoryModelList.size(); j++) {
                if (complaintModels.get(i).getIndustryIndex() != null && complaintModels.get(i).getIndustryIndex().equals(categoryModelList.get(j).getId())) {
                    complaintModels.get(i).setIndustryText(categoryModelList.get(j).getName());
                }
                if (complaintModels.get(i).getReclassifyIndustry() != null && complaintModels.get(i).getReclassifyIndustry().equals(categoryModelList.get(j).getId())) {
                    complaintModels.get(i).setReclassifyIndustryText(categoryModelList.get(j).getName());
                }
            }
        }
        int count = complaintMapper.getComplaintCount(keyword, startTime, endTime,isJiuquan, clue, status, examine, warningStatus);
        ComplainVo vo = new ComplainVo();
        vo.setList(complaintModels);
        vo.setCount(count);
        return vo;
    }

    @Override
    public Integer selectNoGreenLightCount(AdminUserModel user) {
        Integer count = complaintMapper.selectNoGreenLightCount(user.getId());
        return count;
    }

    @Override
    public ComplainVo selectNoGreenLightList(Integer page, Integer limit, AdminUserModel user) {
        List<ComplaintModel> complaintModels = complaintMapper.selectNoGreenLightList(page, limit);
        Integer count = complaintMapper.selectNoGreenLightCount(user.getId());
        ComplainVo vo = new ComplainVo();
        vo.setList(complaintModels);
        vo.setCount(count);
        return vo;
    }

    @Override
    public List<UserModel> whiteList() {
        return complaintMapper.whiteList();
    }

    @Override
    public List<ComplaintModel> examineComplaintList(Integer status, Integer examine) {
        QueryWrapper<ComplaintModel> queryWrapper = new QueryWrapper<>();
        if (status != null && status != 0) {
            queryWrapper.eq("status", status);
        }
        if (examine != null && examine != 0) {
            queryWrapper.eq("examine", examine);
        }
        if (status == null || status == 0) {
            queryWrapper = null;
        }
        QueryWrapper<CategoryModel> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("pid", 10);
        List<ComplaintModel> complaintModels = complaintMapper.selectList(queryWrapper);
        for (int i = 0; i < complaintModels.size(); i++) {
            if (complaintModels.get(i).getStatus() == 1) {
                complaintModels.get(i).setStatusText("待受理");
            }
            if (complaintModels.get(i).getStatus() == 2) {
                complaintModels.get(i).setStatusText("已办理");
            }
            if (complaintModels.get(i).getStatus() == 4) {
                complaintModels.get(i).setStatusText("受理中");
            }
        }
        return complaintModels;
    }

    @Override
    public List<ComplaintModel> examineComplaintList(Integer status, Integer adminId, Integer examine) {
        QueryWrapper<ComplaintModel> queryWrapper = new QueryWrapper<>();
        if (status == null || status == 0) {
            queryWrapper.select();
        }
        if (status != null && status != 0) {
            queryWrapper.eq("status", status);
        }
        if (examine != null && examine != 0) {
            queryWrapper.eq("examine", examine);
        }
        if (adminId != null) {
            queryWrapper.eq("admin_user_id", adminId);
        }
        List<ComplaintModel> complaintModels = complaintMapper.selectList(queryWrapper);
        for (int i = 0; i < complaintModels.size(); i++) {
            complaintModels.get(i).setAdminId(adminId);
            if (complaintModels.get(i).getExamine() != null && complaintModels.get(i).getExamine() != 1 && complaintModels.get(i).getStatus() != 1) {
                complaintModels.get(i).setStatus(4);
            }
            if (complaintModels.get(i).getStatus() == 1) {
                complaintModels.get(i).setStatusText("待受理");
            }
            if (complaintModels.get(i).getStatus() == 2) {
                complaintModels.get(i).setStatusText("已办理");
            }
            if (complaintModels.get(i).getStatus() == 4) {
                complaintModels.get(i).setStatusText("受理中");
            }
        }
        return complaintModels;
    }

    @Override
    public Object selectHandle(Integer id) {
        ComplaintModel model = complaintMapper.selectById(id);
        QueryWrapper<AdminUserModel> queryWrapper = new QueryWrapper<>();
        if (model != null && model.getAdminUserId() != null) {
            String[] split = model.getAdminUserId().split(",");
            queryWrapper.select("id", "real_name", "roles").in("id", split);
            List<AdminUserModel> adminUserModels = adminUserMapper.selectList(queryWrapper);
            if (adminUserModels != null) {
                for (int i = 0; i < adminUserModels.size(); i++) {
                    RoleModel roleByRoleId = roleService.getRoleByRoleId(adminUserModels.get(i).getRoles());
                    adminUserModels.get(i).setRoleName(roleByRoleId.getRoleName());
                }
            }
            model.setAdminUserModels(adminUserModels);
        }
        QueryWrapper<ComplainUrlModel> queryWrapper1 = new QueryWrapper<>();
        assert model != null;
        queryWrapper1.eq("complain_id", model.getId()).orderByDesc("id");
        List<ComplainUrlModel> complainUrlModels = complaintUrlMapper.selectList(queryWrapper1);
        for (int i = 0; i < complainUrlModels.size(); i++) {
            if (complainUrlModels.get(i).getType() == 1) {
                model.setPicUrl(complainUrlModels.get(i).getUrl());
            }
            if (complainUrlModels.get(i).getType() == 2) {
                model.setVideoUrl(complainUrlModels.get(i).getUrl());
            }
            if (complainUrlModels.get(i).getType() == 3) {
                model.setAudioUrl(complainUrlModels.get(i).getUrl());
            }
            if (complainUrlModels.get(i).getType() == 4) {
                model.setPdfUrl(complainUrlModels.get(i).getUrl());
            }
        }
        return model;
    }

    @Override
    public List<ComplaintModel> selectByUserId(Integer id) {
        QueryWrapper<ComplaintModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id).orderByDesc("id");
        List<ComplaintModel> complaintModels = complaintMapper.selectList(queryWrapper);
        QueryWrapper<CategoryModel> queryWrapper1 = new QueryWrapper<>();
        queryWrapper.eq("pid", 10);
        List<CategoryModel> categoryModelList = categoryMapper.selectList(queryWrapper1);
        for (int i = 0; i < complaintModels.size(); i++) {
            for (int i1 = 0; i1 < categoryModelList.size(); i1++) {
                if (complaintModels.get(i).getIndustryIndex() != null && complaintModels.get(i).getIndustryIndex().equals(categoryModelList.get(i1).getId())) {
                    complaintModels.get(i).setIndustryText(categoryModelList.get(i1).getName());
                }
                if (complaintModels.get(i).getReclassifyIndustry() != null && complaintModels.get(i).getReclassifyIndustry().equals(categoryModelList.get(i1).getId())) {
                    complaintModels.get(i).setReclassifyIndustryText(categoryModelList.get(i1).getName());
                }
            }
        }
        return complaintModels;
    }

    @Override
    public int selectAllCount(AdminUserModel user) {
        return complaintMapper.selectAllCount(user.getId());

    }

    @Override
    public int selectStatusCount(AdminUserModel user) {
        return complaintMapper.selectStatusCount(user.getId());
    }

    @Override
    public int selectByType(Integer type, Integer adminId, Integer roles) {
        return complaintMapper.selectByType(type, adminId, roles);
    }

    @Override
    public int selectByStatus(Integer status, Integer adminId, Integer roles) {
        return complaintMapper.selectByStatus(status, adminId, roles);
    }

    @Override
    public int selectIndustryCount(Integer id, Integer adminId, Integer roles) {
        return complaintMapper.selectIndustryCount(id, adminId, roles);
    }

    @Override
    public void examine(Integer id, Integer examine, String examineRemark) {
        ComplaintModel model = complaintMapper.selectById(id);
        if (model != null) {
            if (examine == 2) {
                model.setStatus(4);
            }
            model.setExamine(examine);
            model.setExamineRemark(examineRemark);
            complaintMapper.updateById(model);
            if (model.getIsJiuquan() != null) {
                try {
                    applicationContext.publishEvent(new ComplaintEvent(model));
                }catch (Exception e){
                    log.info(e.toString());
                }
            }
        }
    }

    @Override
    public String updateClue(Integer id, Integer clue) {
        ComplaintModel model = complaintMapper.selectById(id);
        if (model != null) {
            model.setClue(clue);
            complaintMapper.updateById(model);
            if (model.getIsJiuquan() != null) {
                try {
                    applicationContext.publishEvent(new ComplaintEvent(model));
                }catch (Exception e){
                    log.info(e.toString());
                }
            }
            return "操作成功";
        } else {
            return "该条投诉不存在";
        }
    }

    @Override
    public String updateRecord(Integer id, String record) {
        ComplaintModel model = complaintMapper.selectById(id);
        if (model != null) {
            model.setRecord(record);
            complaintMapper.updateById(model);
            if (model.getIsJiuquan() != null) {
                try {
                    applicationContext.publishEvent(new ComplaintEvent(model));
                }catch (Exception e){
                    log.info(e.toString());
                }
            }
            return record;
        } else {
            return "该条投诉不存在";
        }
    }

    @Override
    public void pushStatus(ComplaintModel source) {
        try {
            String result2 = HttpRequest.post("http://127.0.0.1:9111/jiuquan/complaint/doProtectionState")
                    .header(Header.USER_AGENT, "Hutool http")//头信息，多个头信息多次调用此方法即可
                    .header(Header.CONTENT_TYPE,"application/json")
                    .body(JSON.toJSONString(source))//表单内容
                    .timeout(20000)//超时，毫秒
                    .execute().body();
            Console.log(result2);
        }catch (Exception e){
            Console.log(e.toString());
        }
    }

    @Override
    public ComplaintModel getById(Integer id) {
        ComplaintModel complaintModel = complaintMapper.selectById(id);
        QueryWrapper<CategoryModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", 10);
        List<CategoryModel> categoryModelList = categoryMapper.selectList(queryWrapper);
        if (complaintModel.getCircleStatus() != null && complaintModel.getCircleStatus() == 1) {
            complaintModel.setCircleStatusText("当场支付");
        } else if (complaintModel.getCircleStatus() != null && complaintModel.getCircleStatus() == 2) {
            complaintModel.setCircleStatusText("达成支付协议");
        } else if (complaintModel.getCircleStatus() != null && complaintModel.getCircleStatus() == 3) {
            complaintModel.setCircleStatusText("书面投诉立案");
        } else if (complaintModel.getCircleStatus() != null && complaintModel.getCircleStatus() == 4) {
            complaintModel.setCircleStatusText("告知其他途径");
        }
        for (CategoryModel categoryModel : categoryModelList) {
            if (complaintModel.getIndustryIndex() != null && complaintModel.getIndustryIndex().equals(categoryModel.getId())) {
                complaintModel.setIndustryText(categoryModel.getName());
            }
            if (complaintModel.getReclassifyIndustry() != null && complaintModel.getReclassifyIndustry().equals(categoryModel.getId())) {
                complaintModel.setReclassifyIndustryText(categoryModel.getName());
            }
        }
        if (complaintModel.getProcessedId() != null && complaintModel.getProcessedId() != 1) {
            Integer adminId = complaintModel.getProcessedId();
            AdminUserModel adminUserModel = adminUserMapper.selectById(adminId);
            UserModel userModel = userMapper.selectById(adminUserModel.getUserId());
            if (userModel != null) {
                complaintModel.setProcessedName(userModel.getUserName());
                complaintModel.setProcessedMobile(userModel.getMobile());
            }
        } else if (complaintModel.getProcessedId() != null && complaintModel.getProcessedId() == 1) {
            complaintModel.setProcessedName("超级管理员");
            complaintModel.setProcessedMobile("");
        }
        if (complaintModel.getStatus() == 1) {
            complaintModel.setStatusText("待受理");
        }
        if (complaintModel.getStatus() == 2) {
            complaintModel.setStatusText("已办理");
        }
        if (complaintModel.getStatus() == 4) {
            complaintModel.setStatusText("受理中");
        }
        if (complaintModel.getType() != null) {
            if (complaintModel.getType() == 2) {
                complaintModel.setTypeText("⾮⼯程建设领域");
            }
            if (complaintModel.getType() == 1) {
                complaintModel.setTypeText("⼯程建设领域");
            }
        }
        if (complaintModel.getIsContract() != null) {
            if (complaintModel.getIsContract() == 1) {
                complaintModel.setIsContractText("是");
            }
            if (complaintModel.getIsContract() == 2) {
                complaintModel.setIsContractText("否");
            }
        }
        return complaintModel;
    }
}
